$(document).ready(function () {		
		$('#cost_datebox').datetimebox('setValue', formatterDate(new Date()));	
})	
//得到当前日期
	formatterDate = function (date) {
		var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
		var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
		+ (date.getMonth() + 1);
		var hor = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
		};
	/*数据表格行编辑*/
	var editIndex1 = undefined;
	function endEditing(){
		if (editIndex1 == undefined){return true}
		if ($('#cost').datagrid('validateRow', editIndex1)){
			$('#cost').datagrid('endEdit', editIndex1);
				editIndex1 = undefined;
				return true;
		} else {
			return false;
		}
	}
	function onClickRow(index){
		if (editIndex1 != index){
			if (endEditing()){
				$('#cost').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
					editIndex1 = index;
			} else {
				$('#cost').datagrid('selectRow', editIndex1);
			}
		}
	}
/*数据表格行删除*/	
	function  go(val,row){
		return "<a href='#'  class='button-delete button-danger  l-btn l-btn-small' onclick='deleterow(this)' ><span class='l-btn-left' ><span class='l-btn-text'>删除</span></span></a>";	
	} 
	function getRowIndex(target){
		var tr = $(target).closest('tr.datagrid-row');
		return parseInt(tr.attr('datagrid-row-index'));
	}
	function deleterow(target){
		$.messager.confirm('Confirm','确定删除？',function(r){
			if (r){
				$('#cost').datagrid('deleteRow', getRowIndex(target));
			}
		});
	}
	//点击添加
	function append()
	{		
		var reData ={
				zctime:$('#cost_datebox').combobox('getText'),
				zctypeids:$('#fyzc_select').combobox('getValue'),
				zcfsids:$('#skfs_select').combobox('getValue'),
				zcryids:$('#zcry_select').combobox('getValue'),
				zctype:$('#fyzc_select').combobox('getText'),
				zcfs:$('#skfs_select').combobox('getText'),
				zcry:$('#zcry_select').combobox('getText'),
				money:0,
				detail:""
		}		
		$('#cost').datagrid('insertRow',{row:reData});				
	}
	$(document).keydown(function(event){ 
		if(event.keyCode == 13){
			$('#cost').datagrid("endEdit", editIndex1);
		} 
	});
	//保存
	function  save()
	{	
		
		/*把所有数据都拼接到form表单里*/
		$("#allData").html("");
		var rows = $('#cost').datagrid('getRows');
		for(var i=0;i<rows.length;i++)
		{	
			var rowIndex = $('#cost').datagrid('getRowIndex', rows[i]);
			 $('#cost').datagrid('endEdit', rowIndex);
			var zctime = rows[i].zctime;
			var zctypeids = rows[i].zctypeids;
			var money = rows[i].money;
			var detail = rows[i].detail;
			var zcfsids = rows[i].zcfsids;
			var zcryids = rows[i].zcryids;
			$("#allData").append("<input type='hidden' name='sbPayList["+i+"].date' value='"+zctime+"'/>");
			$("#allData").append("<input type='hidden' name='sbPayList["+i+"].type' value='"+zctypeids+"'/>");
			$("#allData").append("<input type='hidden' name='sbPayList["+i+"].price' value='"+money+"'/>");
			$("#allData").append("<input type='hidden' name='sbPayList["+i+"].zcmx' value='"+detail+"'/>");
			$("#allData").append("<input type='hidden' name='sbPayList["+i+"].exUserIds' value='"+zcryids+"'/>");
			$("#allData").append("<input type='hidden' name='sbPayList["+i+"].zffs' value='"+zcfsids+"'/>");
		}		
			var url='/bcost/saveBcost';
			var data=$('#allData_form').serialize();
			var reData = common_ajax.ajaxFunc(url,data);
			if(reData == "200")
			{
				 $.slideMsg("保存成功", parent.$);
				 parent.refresh();
			}else
			{
				$.alert(parent.$, reData);
			}
	}